package top.yongyan0624.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import top.yongyan0624.pojo.Room;
import top.yongyan0624.service.RoomService;

import java.util.List;

/**
 * @author Yongyan
 * @description: TODO
 * @date 2020/12/23
 **/

@Controller
@RequestMapping("/room")
@SessionAttributes("error")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @RequestMapping("/toAddRoomPage")
    public String toAddRoomPage(Model model){
        List<Room> roomList = roomService.findAllRoom();
        model.addAttribute("roomList",roomList);
        model.addAttribute("error","");

        return "addRoomPage";
    }

    @RequestMapping("/addRoom")
    public String addRoom(Room room, Model model){
        Room c = roomService.findById(room.getRno());
        if (c == null){
            roomService.addRoom(room);
        } else {
            model.addAttribute("error","该教室编号已被录入！");
            return "addRoomPage";
        }
        return "redirect:/room/toFindAllRoomPage/1/5";
    }

    @RequestMapping("/deleteRoom/{id}")
    public String deleteRoom(@PathVariable("id") Integer id){
        roomService.deleteRoom(id);
        return "redirect:/room/toFindAllRoomPage/1/5";
    }

    @RequestMapping("/toUpdateRoomPage/{id}")
    public String toUpdateRoomPage(@PathVariable Integer id, Model model){
        Room room = roomService.findById(id);
        model.addAttribute("room",room);
        List<Room> roomList = roomService.findAllRoom();
        model.addAttribute("roomList",roomList);

        return "updateRoomPage";
    }

    @RequestMapping("/updateRoom")
    public String updateRoom(Room room){
        roomService.updateRoom(room);
        return "redirect:/room/toFindAllRoomPage/1/5";
    }

    @RequestMapping("/toFindAllRoomPage/{pageNum}/{pageSize}")
    public String toFindAllPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize,Model model){
        PageInfo<Room> pageInfo = roomService.findAllRoom(pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("roomList",pageInfo.getList());
        model.addAttribute("error","");

        return "findAllRoomPage";
    }


    @RequestMapping("/findRoom")
    public String findRoom(Room room,Model model){
        List<Room> roomList = roomService.findRoom(room);

        if (roomList.size()<=0){
            model.addAttribute("error","未找到该教室...");

        } else if (room.getRno() == null && room.getFlag() == null) {
            model.addAttribute("error","");

        } else {
            model.addAttribute("roomList",roomList);
            model.addAttribute("error","");
            return "findAllRoomPage";
        }

        return "redirect:/room/toFindAllRoomPage/1/5";
    }

}
